package cn.edu.zjicm.wordsnet_d.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.TextView;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.WordsBookActivity;
import cn.edu.zjicm.wordsnet_d.app.WordApp.OnWordChangeListener;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.data.StaticData;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.util.DateTimeUtils;
import cn.edu.zjicm.wordsnet_d.view.pageindicator.PageIndicator;

public class LearnedWordsPagerAdapter extends PagerAdapter implements OnWordChangeListener {
	private WordsBookActivity activity;

	private ArrayList<OneWord> mUnFamiliarList, mFamiliarList, mTooEasyList;
	private int mLastItemUnFamiliarList, mLastItemFamiliarList, mLastItemTooEasyList;

	// 两个列表，生词/熟词
	private ListView mGroupListView[] = new ListView[3];
	private TextView hintTextvView[]=new  TextView[3];
	private WordListAdapter mGroupAdapter[] = new WordListAdapter[3];
	private WordFactory wf;
//	private PageIndicator mPageIndicator;

	public LearnedWordsPagerAdapter(WordsBookActivity activity, PageIndicator pageIndicator) {
		this.activity = activity;
//		mPageIndicator = pageIndicator;
		wf = WordFactory.getInstance(activity);
		mUnFamiliarList = new ArrayList<OneWord>();
		mFamiliarList = new ArrayList<OneWord>();
		mTooEasyList = new ArrayList<OneWord>();
		mUnFamiliarList.addAll(wf.getUnFamiliarWordsLimit(0, StaticData.BufferSize));
		mFamiliarList.addAll(wf.getFamiliarWordsLimit(0, StaticData.BufferSize));
		mTooEasyList.addAll(wf.getTooEasyWordsLimit(0, StaticData.BufferSize));
		init();
	}

	@Override
	public int getCount() {
		return 3;
	}

	@Override
	public boolean isViewFromObject(View view, Object obj) {
		return view == obj;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		LayoutInflater mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		switch (position) {
		case 0: {
			View unFamiliarWordView = mInflater.inflate(R.layout.view_date_unit_list, null);
			mGroupListView[0] = (ListView) unFamiliarWordView.findViewById(R.id.word_list1);
			hintTextvView[0]=(TextView) unFamiliarWordView.findViewById(R.id.difficulty_tip);
			if(mUnFamiliarList.size()==0)
			{
				hintTextvView[0].setVisibility(View.VISIBLE);
				hintTextvView[0].setText("你现在还没有任何相关单词哦");
				mGroupListView[0].setVisibility(View.GONE);
			}
			else
			{
				hintTextvView[0].setVisibility(View.GONE);
				mGroupListView[0].setVisibility(View.VISIBLE);
			}
			mGroupAdapter[0] = new WordListAdapter(activity, mUnFamiliarList);
			mGroupListView[0].setOnScrollListener(new OnScrollListener() {
				@Override
				public void onScrollStateChanged(AbsListView view, int scrollState) {
					if (mLastItemUnFamiliarList == mGroupAdapter[0].getCount()) {
						if (loadMoreUnFamiliarWords()) {
							mGroupAdapter[0].notifyDataSetChanged();
						}
					}
				}

				@Override
				public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
					mLastItemUnFamiliarList = firstVisibleItem + visibleItemCount;
				}
			});
			mGroupListView[0].setAdapter(mGroupAdapter[0]);
			container.addView(unFamiliarWordView);
			return unFamiliarWordView;
		}
		case 1: {
			View familiarWordView = mInflater.inflate(R.layout.view_date_unit_list, null);
			mGroupListView[1] = (ListView) familiarWordView.findViewById(R.id.word_list1);
			hintTextvView[1]=(TextView) familiarWordView.findViewById(R.id.difficulty_tip);
			if(mFamiliarList.size()==0)
			{
				hintTextvView[1].setVisibility(View.VISIBLE);
				hintTextvView[1].setText("你现在还没有任何相关单词哦");
				mGroupListView[1].setVisibility(View.GONE);
			}
			else
			{
				hintTextvView[1].setVisibility(View.GONE);
				mGroupListView[1].setVisibility(View.VISIBLE);
			}
			mGroupAdapter[1] = new WordListAdapter(activity, mFamiliarList);
			mGroupListView[1].setOnScrollListener(new OnScrollListener() {

				@Override
				public void onScrollStateChanged(AbsListView view, int scrollState) {
					if (mLastItemFamiliarList == mGroupAdapter[1].getCount()) {
						if (loadMoreFamiliarWords()) {
							mGroupAdapter[1].notifyDataSetChanged();
						}
					}
				}

				@Override
				public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
					mLastItemFamiliarList = firstVisibleItem + visibleItemCount;
				}
			});
			mGroupListView[1].setAdapter(mGroupAdapter[1]);
			container.addView(familiarWordView);
			return familiarWordView;
		}
		case 2: {
			View tooEasyWordView = mInflater.inflate(R.layout.view_date_unit_list, null);
			mGroupListView[2] = (ListView) tooEasyWordView.findViewById(R.id.word_list1);
			hintTextvView[2]=(TextView) tooEasyWordView.findViewById(R.id.difficulty_tip);
			if(mTooEasyList.size()==0)
			{
				hintTextvView[2].setVisibility(View.VISIBLE);
				hintTextvView[2].setText("你现在还没有任何相关单词哦");
				mGroupListView[2].setVisibility(View.GONE);
			}
			else
			{
				hintTextvView[2].setVisibility(View.GONE);
				mGroupListView[2].setVisibility(View.VISIBLE);
			}
			mGroupAdapter[2] = new WordListAdapter(activity, mTooEasyList);
			mGroupListView[2].setOnScrollListener(new OnScrollListener() {

				@Override
				public void onScrollStateChanged(AbsListView view, int scrollState) {
					if (mLastItemTooEasyList == mGroupAdapter[2].getCount()) {
						if (loadMoreTooEasyWords()) {
							mGroupAdapter[2].notifyDataSetChanged();
						}
					}
				}

				@Override
				public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
					mLastItemTooEasyList = firstVisibleItem + visibleItemCount;
				}
			});
			mGroupListView[2].setAdapter(mGroupAdapter[2]);
			container.addView(tooEasyWordView);
			return tooEasyWordView;
		}
		}
		return null;
	}

	private void init() {
//		mPageIndicator.setOnPageChangeListener(new OnPageChangeListener() {
//			@Override
//			public void onPageScrollStateChanged(int arg0) {
//			}
//
//			@Override
//			public void onPageScrolled(int arg0, float arg1, int arg2) {
//			}
//
//			@Override
//			public void onPageSelected(final int arg0) {
//				activity.setCurrentViewPagerIndex(arg0);
//			}
//		});
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		// TODO Auto-generated method stub
		container.removeView((View) object);
	}

	@Override
	public CharSequence getPageTitle(int position) {
		switch (position) {
		case 0:
			return activity.getString(R.string.unfamiliar_words_list);
		case 1:
			return activity.getString(R.string.familiar_words_list);
		case 2:
			return activity.getString(R.string.too_easy_words_list);
		}
		return super.getPageTitle(position);
	}

	@Override
	public int getItemPosition(Object object) {
		// TODO Auto-generated method stub
		return POSITION_NONE;
	}

	@Override
	public void onWordDegreeChange(int oldDegree, OneWord word) {
		switch (oldDegree) {
		case OneWord.DEGREE_UNLEARN:
			// mUnLnWordAdapter.remove(word);
			break;
		case OneWord.DEGREE_UNFAMILIAR:
			mGroupAdapter[0].remove(word);
			break;
		case OneWord.DEGREE_FAMILIAR:
			mGroupAdapter[1].remove(word);
			break;
		}
		word.setTime_set(DateTimeUtils.getDate());
		switch (word.getDegree()) {
		case OneWord.DEGREE_UNLEARN:
			// mUnLnWordAdapter.add(word);
			break;
		case OneWord.DEGREE_UNFAMILIAR:
			mGroupAdapter[0].add(word);
			break;
		case OneWord.DEGREE_FAMILIAR:
			mGroupAdapter[1].add(word);
			break;
		}
	}

	@Override
	public void onInstantiateItem(int position) {
		mGroupListView[position].invalidateViews();
	}

	@Override
	public void onAdapterReset(int position) {

	}

	public void resetListForEdit(int page) {
		mGroupAdapter[page].notifyDataSetChanged();
	}

	public ListView[] getWordList() {
		return mGroupListView;
	}

	private boolean loadMoreUnFamiliarWords() {
		int listSize = mUnFamiliarList.size();
		List<OneWord> loadedList = wf.getUnFamiliarWordsLimit(listSize, StaticData.BufferSize);
		if (null == loadedList)
			return false;
		mUnFamiliarList.addAll(loadedList);
		return true;
	}

	private boolean loadMoreFamiliarWords() {
		int listSize = mFamiliarList.size();
		List<OneWord> loadedList = wf.getFamiliarWordsLimit(listSize, StaticData.BufferSize);
		if (null == loadedList)
			return false;
		mFamiliarList.addAll(loadedList);
		return true;
	}

	private boolean loadMoreTooEasyWords() {
		int listSize = mTooEasyList.size();
		List<OneWord> loadedList = wf.getTooEasyWordsLimit(listSize, StaticData.BufferSize);
		if (null == loadedList)
			return false;
		mTooEasyList.addAll(loadedList);
		return true;
	}

	public void reLoadList() {
		mUnFamiliarList.clear();
		mFamiliarList.clear();
		mTooEasyList.clear();
		mUnFamiliarList.addAll(wf.getUnFamiliarWordsLimit(0, StaticData.BufferSize));
		mFamiliarList.addAll(wf.getFamiliarWordsLimit(0, StaticData.BufferSize));
		mTooEasyList.addAll(wf.getTooEasyWordsLimit(0, StaticData.BufferSize));

		if(mUnFamiliarList.size()==0)
		{
			hintTextvView[0].setVisibility(View.VISIBLE);
			hintTextvView[0].setText("你现在还没有任何相关单词哦");
			mGroupListView[0].setVisibility(View.GONE);
		}
		else
		{
			hintTextvView[0].setVisibility(View.GONE);
			mGroupListView[0].setVisibility(View.VISIBLE);
		}
		if(mFamiliarList.size()==0)
		{
			hintTextvView[1].setVisibility(View.VISIBLE);
			hintTextvView[1].setText("你现在还没有任何相关单词哦");
			mGroupListView[1].setVisibility(View.GONE);
		}
		else
		{
			hintTextvView[1].setVisibility(View.GONE);
			mGroupListView[1].setVisibility(View.VISIBLE);
		}
		if(mTooEasyList.size()==0)
		{
			hintTextvView[2].setVisibility(View.VISIBLE);
			hintTextvView[2].setText("你现在还没有任何相关单词哦");
			mGroupListView[2].setVisibility(View.GONE);
		}
		else
		{
			hintTextvView[2].setVisibility(View.GONE);
			mGroupListView[2].setVisibility(View.VISIBLE);
		}
		for (int i = 0; i < mGroupAdapter.length; i++)
			mGroupAdapter[i].notifyDataSetChanged();

	}
}